home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_147 / sys / sysv / sysv.zoo / fileio.c next >
C/C++ Source or Header  |  1988-07-25  |  9KB  |  421 lines

  1. /*
  2.  *        sys V fileio.c
  3.  */
  4. #include    "def.h"
  5.  
  6. static    FILE    *ffp;
  7. extern    char    *getenv();
  8. char    *adjustname();
  9.  
  10. #include <sys/types.h>
  11.  
  12. /*
  13.  * Open a file for reading.
  14.  */
  15. ffropen(fn) char *fn; {
  16.     if ((ffp=fopen(fn, "r")) == NULL)
  17.         return (FIOFNF);
  18.     return (FIOSUC);
  19. }
  20.  
  21. /*
  22.  * Open a file for writing.
  23.  * Return TRUE if all is well, and
  24.  * FALSE on error (cannot create).
  25.  */
  26. ffwopen(fn) char *fn; {
  27.     if ((ffp=fopen(fn, "w")) == NULL) {
  28.         ewprintf("Cannot open file for writing");
  29.         return (FIOERR);
  30.     }
  31.     return (FIOSUC);
  32. }
  33.  
  34. /*
  35.  * Close a file.
  36.  * Should look at the status.
  37.  */
  38. ffclose() {
  39.     (VOID) fclose(ffp);
  40.     return (FIOSUC);
  41. }
  42.  
  43. /*
  44.  * Write a buffer to the already
  45.  * opened file. bp points to the
  46.  * buffer. Return the status.
  47.  * Check only at the newline and
  48.  * end of buffer.
  49.  */
  50. ffputbuf(bp)
  51. BUFFER *bp;
  52. {
  53.     register char *cp;
  54.     register char *cpend;
  55.     register LINE *lp;
  56.     register LINE *lpend;
  57.  
  58.     lpend = bp->b_linep;
  59.     lp = lforw(lpend);
  60.     do {
  61.     cp = <ext(lp)[0];        /* begining of line    */
  62.     cpend = &cp[llength(lp)];    /* end of line        */
  63.     while(cp != cpend) {
  64.         putc(*cp, ffp);
  65.         cp++;    /* putc may evalualte arguments more than once */
  66.     }
  67.     lp = lforw(lp);
  68.     if(lp == lpend) break;        /* no implied newline on last line */
  69.     putc('\n', ffp);
  70.     } while(!ferror(ffp));
  71.     if(ferror(ffp)) {
  72.     ewprintf("Write I/O error");
  73.     return FIOERR;
  74.     }
  75.     return FIOSUC;
  76. }
  77.  
  78. /*
  79.  * Read a line from a file, and store the bytes
  80.  * in the supplied buffer. Stop on end of file or end of
  81.  * line.  When FIOEOF is returned, there is a valid line
  82.  * of data without the normally implied \n.
  83.  */
  84. ffgetline(buf, nbuf, nbytes)
  85. register char    *buf;
  86. register int    nbuf;
  87. register int    *nbytes;
  88. {
  89.     register int    c;
  90.     register int    i;
  91.  
  92.     i = 0;
  93.     while((c = getc(ffp))!=EOF && c!='\n') {
  94.         buf[i++] = c;
  95.         if (i >= nbuf) return FIOLONG;
  96.     }
  97.     if (c == EOF  && ferror(ffp) != FALSE) {
  98.         ewprintf("File read error");
  99.         return FIOERR;
  100.     }
  101.     *nbytes = i;
  102.     return c==EOF ? FIOEOF : FIOSUC;
  103. }
  104.  
  105. #ifndef NO_BACKUP
  106. /*
  107.  * Rename the file "fname" into a backup
  108.  * copy. On Unix the backup has the same name as the
  109.  * original file, with a "~" on the end; this seems to
  110.  * be newest of the new-speak. The error handling is
  111.  * all in "file.c". The "unlink" is perhaps not the
  112.  * right thing here; I don't care that much as
  113.  * I don't enable backups myself.
  114.  */
  115. fbackupfile(fn) char *fn; {
  116.     register char    *nname;
  117.     char        *malloc();
  118.  
  119.     if ((nname=malloc((unsigned)(strlen(fn)+1+1))) == NULL) {
  120.         ewprintf("Can't get %d bytes", strlen(fn) + 1);
  121.         return (ABORT);
  122.     }
  123.     (void) strcpy(nname, fn);
  124.     (void) strcat(nname, "~");
  125.     (void) unlink(nname);            /* Ignore errors.    */
  126.     if (rename(fn, nname) < 0) {
  127.         free(nname);
  128.         return (FALSE);
  129.     }
  130.     free(nname);
  131.     return (TRUE);
  132. }
  133. #endif
  134.  
  135. /*
  136.  * The string "fn" is a file name.
  137.  * Perform any required appending of directory name or case adjustments.
  138.  * If NO_DIR is not defined, the same file should be refered to even if the
  139.  * working directory changes.
  140.  */
  141. #ifdef SYMBLINK
  142. #include <sys/types.h>
  143. #include <sys/stat.h>
  144. #ifndef MAXLINK
  145. #define MAXLINK 8        /* maximum symbolic links to follow */
  146. #endif
  147. #endif
  148. #include <pwd.h>
  149. #ifndef NO_DIR
  150. extern char *wdir;
  151. #endif
  152.  
  153. char *adjustname(fn)
  154. register char *fn;
  155. {
  156.     register char *cp;
  157.     static char fnb[NFILEN];
  158.     struct passwd *pwent;
  159. #ifdef    SYMBLINK
  160.     struct stat statbuf;
  161.     int i, j;
  162.     char linkbuf[NFILEN];
  163. #endif
  164.  
  165.     switch(*fn) {
  166.         case '/':
  167.         cp = fnb;
  168.         *cp++ = *fn++;
  169.         break;
  170.     case '~':
  171.         fn++;
  172.         if(*fn == '/' || *fn == '\0') {
  173.         (VOID) strcpy(fnb, getenv("HOME"));
  174.         cp = fnb + strlen(fnb);
  175.             if(*fn) fn++;
  176.         break;
  177.         } else {
  178.         cp = fnb;
  179.         while(*fn && *fn != '/') *cp++ = *fn++;
  180.         *cp = '\0';
  181.         if((pwent = getpwnam(fnb)) != NULL) {
  182.             (VOID) strcpy(fnb, pwent->pw_dir);
  183.             cp = fnb + strlen(fnb);
  184.             break;
  185.         } else {
  186.             fn -= strlen(fnb) + 1;
  187.             /* can't find ~user, continue to default case */
  188.         }
  189.         }
  190.     default:
  191. #ifndef    NODIR
  192.         strcpy(fnb, wdir);
  193.         cp = fnb + strlen(fnb);
  194.         break;
  195. #else
  196.         return fn;                /* punt */
  197. #endif
  198.     }
  199.     if(cp != fnb && cp[-1] != '/') *cp++ = '/';
  200.     while(*fn) {
  201.         switch(*fn) {
  202.         case '.':
  203.         switch(fn[1]) {
  204.                 case '\0':
  205.                 *--cp = '\0';
  206.                 return fnb;
  207.                 case '/':
  208.                     fn += 2;
  209.                 continue;
  210.             case '.':
  211.                 if(fn[2]=='/' || fn[2] == '\0') {
  212. #ifdef SYMBLINK
  213.                 cp[-1] = '\0';
  214.                 for(j = MAXLINK; j-- && 
  215.                         lstat(fnb, &statbuf) != -1 && 
  216.                         (statbuf.st_mode&S_IFMT) == S_IFLNK &&
  217.                         (i = readlink(fnb, linkbuf, sizeof linkbuf))
  218.                     != -1 ;) {
  219.                 if(linkbuf[0] != '/') {
  220.                     --cp;
  221.                     while(cp > fnb && *--cp != '/') {}
  222.                     ++cp;
  223.                     (VOID) strncpy(cp, linkbuf, i);
  224.                     cp += i;
  225.                 } else {
  226.                     (VOID) strncpy(fnb, linkbuf, i);
  227.                     cp = fnb + i;
  228.                 }
  229.                 if(cp[-1]!='/') *cp++ = '\0';
  230.                 else cp[-1] = '\0';
  231.                 }
  232.                 cp[-1] = '/';
  233. #endif
  234.                 --cp;
  235.                 while(cp > fnb && *--cp != '/') {}
  236.                 ++cp;
  237.                 if(fn[2]=='\0') {
  238.                     *--cp = '\0';
  239.                     return fnb;
  240.                 }
  241.                     fn += 3;
  242.                     continue;
  243.                 }
  244.                 break;
  245.             default:
  246.                 break;
  247.             }
  248.         break;
  249.         case '/':
  250.             fn++;
  251.             continue;
  252.         default:
  253.             break;
  254.     }
  255.     while(*fn && (*cp++ = *fn++) != '/') {}
  256.     }
  257.     if(cp[-1]=='/') --cp;
  258.     *cp = '\0';
  259.     return fnb;
  260. }
  261.  
  262. #ifndef NO_STARTUP
  263. #include <sys/file.h>
  264. #define F_OK 04            /* for stupid Sys V        */
  265.  
  266. /*
  267.  * Find a startup file for the user and return its name. As a service
  268.  * to other pieces of code that may want to find a startup file (like
  269.  * the terminal driver in particular), accepts a suffix to be appended
  270.  * to the startup file name.
  271.  */
  272. char *
  273. startupfile(suffix)
  274. char *suffix;
  275. {
  276.     register char    *file;
  277.     static char    home[NFILEN];
  278.     char        *getenv();
  279.  
  280.     if ((file = getenv("HOME")) == NULL) goto notfound;
  281.     if (strlen(file)+7 >= NFILEN - 1) goto notfound;
  282.     (VOID) strcpy(home, file);
  283.     (VOID) strcat(home, "/.mg");
  284.     if (suffix != NULL) {
  285.         (VOID) strcat(home, "-");
  286.         (VOID) strcat(home, suffix);
  287.     }
  288.     if (access(home, F_OK) == 0) return home;
  289.  
  290. notfound:
  291. #ifdef    STARTUPFILE
  292.     file = STARTUPFILE;
  293.     if (suffix != NULL) {
  294.         (VOID) strcpy(home, file);
  295.         (VOID) strcat(home, "-");
  296.         (VOID) strcat(home, suffix);
  297.         file = home;
  298.     }
  299.     if (access(file, F_OK ) == 0) return file;
  300. #endif
  301.  
  302.     return NULL;
  303. }
  304. #endif
  305.  
  306. #ifndef NO_DIRED
  307. #include "kbd.h"
  308.  
  309. copy(frname, toname)
  310. char *frname, *toname;
  311. {
  312.     int pid;
  313.     int status;
  314.  
  315.     if(pid = fork()) {
  316.     if(pid == -1)    return    -1;
  317.     execl("/bin/cp", "cp", frname, toname, (char *)NULL);
  318.     _exit(1);    /* shouldn't happen */
  319.     }
  320.     while(wait(&status) != pid)
  321.     ;
  322.     return status == 0;
  323. }
  324.  
  325. BUFFER *dired_(dirname)
  326. char *dirname;
  327. {
  328.     register BUFFER *bp;
  329.     char line[256];
  330.     BUFFER *findbuffer();
  331.     FILE *dirpipe;
  332.     FILE *popen();
  333.     char *strncpy();
  334.  
  335.     if((dirname = adjustname(dirname)) == NULL) {
  336.     ewprintf("Bad directory name");
  337.     return NULL;
  338.     }
  339.     if((bp = findbuffer(dirname)) == NULL) {
  340.     ewprintf("Could not create buffer");
  341.     return NULL;
  342.     }
  343.     if(bclear(bp) != TRUE) return FALSE;
  344.     (VOID) strcpy(line, "ls -al ");
  345.     (VOID) strcpy(&line[7], dirname);
  346.     if((dirpipe = popen(line, "r")) == NULL) {
  347.     ewprintf("Problem opening pipe to ls");
  348.     return NULL;
  349.     }
  350.     line[0] = line[1] = ' ';
  351.     while(fgets(&line[2], 254, dirpipe) != NULL) {
  352.     line[strlen(line) - 1] = '\0';        /* remove ^J    */
  353.     (VOID) addline(bp, line);
  354.     }
  355.     if(pclose(dirpipe) == -1) {
  356.     ewprintf("Problem closing pipe to ls");
  357.     return NULL;
  358.     }
  359.     bp->b_dotp = lforw(bp->b_linep);        /* go to first line */
  360.     (VOID) strncpy(bp->b_fname, dirname, NFILEN);
  361.     if((bp->b_modes[0] = name_mode("dired")) == NULL) {
  362.     bp->b_modes[0] = &map_table[0];
  363.     ewprintf("Could not find mode dired");
  364.     return NULL;
  365.     }
  366.     bp->b_nmodes = 0;
  367.     return bp;
  368. }
  369.  
  370. d_makename(lp, fn)
  371. register LINE *lp;
  372. register char *fn;
  373. {
  374.     register char *cp;
  375.  
  376.     if(llength(lp) <= 56) return ABORT;
  377.     (VOID) strcpy(fn, curbp->b_fname);
  378.     cp = fn + strlen(fn);
  379.     bcopy(&lp->l_text[56], cp, llength(lp) - 56);
  380.     cp[llength(lp) - 56] = '\0';
  381.     return lgetc(lp, 2) == 'd';
  382. }
  383.  
  384. /*
  385.  * I, a System V novice, could only figure out how to do unlinkdir()
  386.  * and rename() as exec's of the appropriate functions.  So sue me.
  387.  * --Stephen Walton, December 1987
  388.  */
  389.  
  390. unlinkdir(f)
  391. char *f;
  392. {
  393.     int status, pid, wpid;
  394.  
  395.     if ((pid = fork()) == 0)
  396.         execl("/bin/rmdir", "rmdir", f, (char *)NULL);
  397.     else if (pid > 0)
  398.         while ((wpid = wait(&status)) && wpid != pid)
  399.             ;
  400.     else
  401.         return FALSE;
  402.     return status == 0;
  403. }
  404.  
  405. rename(f1, f2)
  406. char *f1, *f2;
  407. {
  408.  
  409.     int status, pid, wpid;
  410.  
  411.     if ((pid = fork()) == 0)
  412.         execl("/bin/mv", "mv", f1, f2, (char *)NULL);
  413.     else if (pid > 0)
  414.         while ((wpid = wait(&status)) && wpid != pid)
  415.             ;
  416.     else
  417.         return FALSE;
  418.     return status == 0;
  419. }
  420. #endif NO_DIRED
  421.